/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BLL;

import DAL.ConnectUnit;
import DTO.MonthlyReport_DTO;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.HashSet;

/**
 *
 * @author Nguyen Phi Hung
 */
public class MonthlyReport_BLL {
    
    ConnectUnit connection;
    
    public MonthlyReport_BLL(){
        this.connection = DAL.DAL.getDAL();
    }
    
    /**
     * get list of MonthlyReport form database with Condition and OrderBy
     * @param condition
     * @param OrderBy
     * @return
     * @throws Exception 
     */
    public HashSet<MonthlyReport_DTO> getMonthlyReports(String condition, String OrderBy) throws Exception{
        ResultSet result = this.connection.Select("MONTHLYREPORT", condition, OrderBy);
        HashSet<MonthlyReport_DTO> monthlyreports = new HashSet<MonthlyReport_DTO>();
        while(result.next()){
            MonthlyReport_DTO monthlyreport = new MonthlyReport_DTO();
            //set info for MonthlyReport
            monthlyreport.setReportID(result.getString("ReportID"));
            monthlyreport.setReportingMonth(result.getDate("ReportingMonth"));
            monthlyreport.setSavingTypeID(result.getString("SavingTypeID"));
            //add into hashset
            monthlyreports.add(monthlyreport);
        }
        return monthlyreports;
    }
    
    /**
     * get list of MonthlyReport form database with Condition
     * @param condition
     * @return
     * @throws Exception 
     */
    public HashSet<MonthlyReport_DTO> getMonthlyReports(String condition) throws Exception{
        return this.getMonthlyReports(condition, null);
    }
    
    /**
     * get list of MonthlyReport form database
     * @return
     * @throws Exception 
     */
    public HashSet<MonthlyReport_DTO> getMonthlyReports() throws Exception{
        return this.getMonthlyReports(null, null);
    }
    
    /**
     * Get MonthlyReport by ReportID
     * @param ID
     * @return
     * @throws Exception 
     */
    public MonthlyReport_DTO getMonthlyReport_byReportID(String ID) throws Exception{
        HashSet<MonthlyReport_DTO> monthlyreports = this.getMonthlyReports("ReportID="+ID);
        if(monthlyreports.size()>=0)
           return monthlyreports.toArray(new MonthlyReport_DTO[monthlyreports.size()])[0];
        return null;    
    }
    
    /**
     * Insert a MonthlyReport into database
     * @param info
     * @throws Exception 
     */
    public void Insert(MonthlyReport_DTO info) throws Exception{
        HashMap<String, Object> map = new HashMap<String, Object>();
        
        map.put("ReportID", info.getReportID());
        map.put("ReportingMonth", info.getReportingMonth());
        map.put("SavingTypeID", info.getSavingTypeID());
        
        String[] arr = {"ReportID","ReportingMonth","SavingTypeID"};
        this.connection.Insert("MONTHLYREPORT", map, arr);
    }
    
    /**
     * Update a MonthlyReport into database
     * @param info
     * @throws Exception 
     */
    public void Update(MonthlyReport_DTO info) throws Exception{
        HashMap<String, Object> map = new HashMap<String, Object>();
        
        map.put("ReportID", info.getReportID());
        map.put("ReportingMonth", info.getReportingMonth());
        map.put("SavingTypeID", info.getSavingTypeID());
        
        this.connection.Update("MONTHLYREPORT", map, "ReportID="+ info.getReportID());
    }
    
    /**
     * Delete MonthlyReport in database
     * @param info
     * @throws Exception 
     */
    public void Delete(MonthlyReport_DTO info) throws Exception{
        this.connection.Delete("MONTHLYREPORT", "ReportID="+ info.getReportID());
    }
}
